package com.yeskery.nut.extend.mcp.server;

import com.yeskery.nut.extend.mcp.core.Constants;
import com.yeskery.nut.http.SseEmitter;
import com.yeskery.nut.http.SseEmitterResponseBodyHandler;

/**
 * MCP sse响应发射器处理器
 * @author sprout
 * 2025-04-17 11:27
 */
public class McpSseEmitterResponseBodyHandler extends SseEmitterResponseBodyHandler {

    /** message端点 */
    private final String messageEndpoint;

    /** 会话id */
    private final String sessionId;

    /**
     * 构造MCP sse响应发射器处理器
     * @param messageEndpoint message端点
     * @param sessionId 会话id
     */
    public McpSseEmitterResponseBodyHandler(String messageEndpoint, String sessionId) {
        this.messageEndpoint = messageEndpoint;
        this.sessionId = sessionId;
    }

    @Override
    public void afterInitialization(SseEmitter sseEmitter) {
        sseEmitter.send(SseEmitter.event().name(Constants.ENDPOINT_EVENT_TYPE).data(messageEndpoint + "?" + Constants.SESSION_ID + "=" + sessionId));
    }
}
